/*
 * Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
 *
 * https://www.mall4j.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */
package com.yami.shop.delivery.common.service.impl;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.bean.app.dto.MyOrderDto;
import com.yami.shop.bean.app.dto.MyOrderItemDto;
import com.yami.shop.bean.app.dto.OrderSelfStationDto;
import com.yami.shop.common.config.Constant;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.delivery.common.dao.OrderSelfStationMapper;
import com.yami.shop.delivery.common.model.OrderSelfStation;
import com.yami.shop.delivery.common.service.OrderSelfStationService;
import com.yami.shop.service.OrderService;
import com.yami.shop.service.OrderVirtualInfoService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 自提订单信息表
 *
 * @author YXF
 * @date 2020-04-23 15:18:29
 */
@Service
@AllArgsConstructor
public class
OrderSelfStationServiceImpl extends ServiceImpl<OrderSelfStationMapper, OrderSelfStation> implements OrderSelfStationService {

    private final OrderService orderService;
    private final OrderSelfStationMapper orderSelfStationMapper;
    private final OrderVirtualInfoService orderVirtualInfoService;

    @Override
    public String getNewInfo(Long shopId) {
        return orderSelfStationMapper.getNewInfo(shopId);
    }


    @Override
    public MyOrderDto getOrderByStationCode(Long stationId, Long shopId, String code) {
        MyOrderDto orderByStationCode = orderSelfStationMapper.getOrderByStationCode(stationId, shopId, code);
        if (Objects.isNull(orderByStationCode)){
            // 如果查询不到，就去查询虚拟商品的券码信息
            orderByStationCode = orderVirtualInfoService.getOrderByCode(shopId,code);
        }
        if (Objects.isNull(orderByStationCode)){
            // 该提货码，没有对应的订单
            throw new YamiShopBindException("yami.order.code.no.exist");
        }
        if (StrUtil.isBlank(orderByStationCode.getShopName())){
            orderByStationCode.setShopName(Constant.PLATFORM_SHOP_NAME);
        }
        Map<Long, List<MyOrderItemDto>> giveawayMap = orderByStationCode.getOrderItemDtos().stream().filter(myOrderItemDto -> Objects.nonNull(myOrderItemDto.getGiveawayOrderItemId())).collect(Collectors.groupingBy(MyOrderItemDto::getGiveawayOrderItemId));
        if (MapUtil.isNotEmpty(giveawayMap)) {
            Iterator<MyOrderItemDto> iterator = orderByStationCode.getOrderItemDtos().iterator();
            while (iterator.hasNext()) {
                MyOrderItemDto myOrderItemDto = iterator.next();
                if (Objects.nonNull(myOrderItemDto.getGiveawayOrderItemId())) {
                    iterator.remove();
                    continue;
                }
                myOrderItemDto.setGiveawayList(giveawayMap.get(myOrderItemDto.getOrderItemId()));
            }
        }
        return orderByStationCode;
    }


    @Override
    public List<OrderSelfStationDto> getUserStation(String userId) {
        return orderSelfStationMapper.getUserStation(userId);
    }

    @Override
    public List<OrderSelfStation> getStationUserInfo(String userId) {
        return orderSelfStationMapper.getStationUserInfo(userId);
    }

    @Override
    public OrderSelfStationDto getStationByOrderNumber(String orderNumber) {
        return orderSelfStationMapper.getStationByOrderNumber(orderNumber);
    }
}
